const mongoose = require("mongoose");
const Problem = require("../models/ProblemModel");

exports.addProblem = async (req, res) => {
  try {
    const newProblem = await Problem.create(req.body);
    res.status(201).json({
      status: "success",
      data: newProblem,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getProblem = async (req, res) => {
  try {
    const ProblemData = await Problem.find();
    let res_data;
    if (typeof ProblemData[0] == "undefined") {
      res_data = {
        title: "问题清单",
        problem: [[], []],
      };
    } else {
      res_data = ProblemData[0];
    }
    res.status(201).json({
      status: "success",
      data: res_data,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.updateProblem = async (req, res) => {
  try {
    const ProblemData = await Problem.updateOne(
      { _id: req.body._id },
      req.body
    );
    res.status(201).json({
      status: "success",
      data: ProblemData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};
